package pers.hl.module_main.search

import android.text.TextUtils
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import kotlinx.android.synthetic.main.item_rv_book.view.*
import pers.hl.common.base.ext.showMessage
import pers.hl.lib_global.common.GlideUtils
import pers.hl.lib_global.model.bean.Book
import pers.hl.lib_global.model.enums.BookStatus
import pers.hl.module_main.R
import pers.hl.module_main.common.BaseLibQuickAdapter
import pers.hl.module_main.common.PageHelper

class BookAdapter(data: MutableList<Book>, private val isAdmin: Boolean= false) :
    BaseLibQuickAdapter<Book, BaseViewHolder>(R.layout.item_rv_book, data) {

    override fun convert(holder: BaseViewHolder, item: Book) {
        val ivSearchBook = holder.itemView.ivSearchBook
        GlideUtils.loadBookImage(ivSearchBook.context, ivSearchBook, item)
        holder.itemView.tvBookName.text = item.bookName
        val subTitle = String.format(
            "%s",
            if (TextUtils.isEmpty(item.bookAuthor)) "佚名" else item.bookAuthor
        )
        holder.itemView.apply {
            tvSearchSubtitle.text = subTitle
            tvStatus.text = item.bookStatus?.let { getStatusName(it) }
            setOnClickListener {
                if (isAdmin) {
                    showMessage("由用户自行修改")
                } else {
                    PageHelper.startBookDetailPage(context, item)
                }
            }
            tvTime.text = item.libName
        }
    }

    private fun getStatusName(bookStatus: Int): String {
        return BookStatus.getValueStr(bookStatus)
    }
}